package com.kwd.news.controller;


import com.kwd.news.model.GlobalConfig;

import com.kwd.news.service.GlobalConfigService;

import com.meirengu.common.StatusCode;
import com.meirengu.model.Page;
import com.meirengu.model.Result;
import com.meirengu.utils.NumberUtil;
import com.meirengu.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("config")
public class GlobalConfigController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalConfigController.class);

    @Autowired
    private GlobalConfigService globalConfigService;

    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result getConfig(@RequestParam(value = "page_size", required = false, defaultValue = "10") int pageSize,
                            @RequestParam(value = "page_num", required = false, defaultValue = "1") int pageNum,
                            @RequestParam(value = "sortby", required = false) String sortBy,
                            @RequestParam(value = "order", required = false) String order,
                            @RequestParam(value = "status", required = false) Integer status) {
        try {
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isNotBlank(sortBy)) {
                map.put("sortBy", sortBy);
            }
            if (!StringUtil.isEmpty(order)) {
                map.put("order", order);
            }
            if (NumberUtil.isNullOrZero(status)) {
                map.put("status", status);
            }
            Page<GlobalConfig> page = new Page<>();
            page.setPageNow(pageNum);
            page.setPageSize(pageSize);
            page = globalConfigService.getListByPage(page, map);
            return super.setResult(StatusCode.OK, page, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            return super.setResult(StatusCode.INTERNAL_SERVER_ERROR, "", StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public Result getConfig(@RequestParam(value = "configKey", required = false) String configKey,
                            @RequestParam(value = "channel", required = false) String channel) {

        if (StringUtil.isEmpty(configKey)) {
            return super.setResult(StatusCode.MISSING_ARGUMENT, "", StatusCode.codeMsgMap.get(StatusCode.MISSING_ARGUMENT));
        }
        try {
            Map<String, String> map = new HashMap<String, String>();
            map.put("globalKey", configKey);
            if (!StringUtil.isEmpty(channel)) {
                map.put("channel", channel);
            }
            GlobalConfig gc = globalConfigService.getGlobalConfig(map);
            return super.setResult(StatusCode.OK, gc, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            return super.setResult(StatusCode.INTERNAL_SERVER_ERROR, "", StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    /**
     * 全局变量详情
     *
     * @return
     */
    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public Result detail(@PathVariable("id") Integer id) {
        if (NumberUtil.isNullOrZero(id)) {
            return setResult(StatusCode.MISSING_ARGUMENT, null, StatusCode.codeMsgMap.get(StatusCode.MISSING_ARGUMENT));
        }
        try {
            GlobalConfig globalConfig = globalConfigService.detail(id);
            return setResult(StatusCode.OK, globalConfig, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    /**
     * 全局变量详情
     *
     * @return
     */
    @RequestMapping(value = "time", method = RequestMethod.GET)
    public Result getServerTime() {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            long time = System.currentTimeMillis();
            map.put("time", time);
            return setResult(StatusCode.OK, map, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            return setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Result addConfig(@RequestParam(value = "configKey", required = false) String configKey,
                            @RequestParam(value = "configValue", required = false) String configValue,
                            @RequestParam(value = "channel", required = false) String channel) {

        if (StringUtil.isEmpty(configKey) || StringUtil.isEmpty(configValue)) {
            return super.setResult(StatusCode.MISSING_ARGUMENT, "", StatusCode.codeMsgMap.get(StatusCode.MISSING_ARGUMENT));
        }
        try {
            Map<String, String> map = new HashMap<String, String>();
            map.put("globalKey", configKey);
            if (!StringUtil.isEmpty(channel)) {
                map.put("channel", channel);
            }
            GlobalConfig gc = globalConfigService.getGlobalConfig(map);
            if (gc != null) {
                return super.setResult(StatusCode.GLOBAL_CONFIG_REPATE_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.GLOBAL_CONFIG_REPATE_ERROR));
            }
            GlobalConfig globalConfig = new GlobalConfig();
            globalConfig.setGlobalKey(configKey);
            globalConfig.setGlobalValue(configValue);
            if (!StringUtil.isEmpty(channel))
                globalConfig.setChannel(channel);
            int flag = globalConfigService.addGlobalConfig(globalConfig);
            if (flag == 1) {
                return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
            } else {
                return super.setResult(StatusCode.GLOBAL_CONFIG_ADD_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.GLOBAL_CONFIG_ADD_ERROR));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return super.setResult(StatusCode.INTERNAL_SERVER_ERROR, "", StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result updateConfig(@RequestParam(value = "id", required = false) Integer id,
                               @RequestParam(value = "configKey", required = false) String configKey,
                               @RequestParam(value = "configValue", required = false) String configValue,
                               @RequestParam(value = "channel", required = false) String channel) {

        if (StringUtil.isEmpty(configKey) || StringUtil.isEmpty(configValue) || StringUtil.isEmpty(id)) {
            return super.setResult(StatusCode.MISSING_ARGUMENT, "", StatusCode.codeMsgMap.get(StatusCode.MISSING_ARGUMENT));
        }
        try {
            GlobalConfig globalConfig = new GlobalConfig();
            globalConfig.setId(id);
            globalConfig.setGlobalKey(configKey);
            globalConfig.setGlobalValue(configValue);
            if (!StringUtil.isEmpty(channel))
                globalConfig.setChannel(channel);
            int flag = globalConfigService.updateGlobalConfig(globalConfig);
            if (flag == 1) {
                return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
            } else {
                return super.setResult(StatusCode.GLOBAL_CONFIG_ADD_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.GLOBAL_CONFIG_ADD_ERROR));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return super.setResult(StatusCode.INTERNAL_SERVER_ERROR, "", StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }
    }

}
